package com.ln.lnzw.activity;

import android.os.Bundle;
import android.os.Handler;
import android.support.annotation.NonNull;
import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.util.Log;
import android.view.View;
import android.widget.ImageView;
import android.widget.TextView;

import com.blankj.utilcode.util.ActivityUtils;
import com.chad.library.adapter.base.BaseQuickAdapter;
import com.chad.library.adapter.base.listener.OnItemClickListener;
import com.ln.lnzw.R;
import com.ln.lnzw.adapter.AnnouncementAdapter;
import com.ln.lnzw.base.BaseActivity;
import com.ln.lnzw.bean.WcmBean;
import com.ln.lnzw.net.HttpMethods;
import com.scwang.smartrefresh.layout.SmartRefreshLayout;
import com.scwang.smartrefresh.layout.api.RefreshLayout;
import com.scwang.smartrefresh.layout.listener.OnLoadMoreListener;
import com.scwang.smartrefresh.layout.listener.OnRefreshListener;

import java.util.ArrayList;
import java.util.List;

import butterknife.BindView;
import butterknife.ButterKnife;
import butterknife.OnClick;
import io.reactivex.Observer;
import io.reactivex.android.schedulers.AndroidSchedulers;
import io.reactivex.disposables.Disposable;
import io.reactivex.schedulers.Schedulers;

/**
 * Created by wuwei on 2018/4/13.
 * 查看更多
 */

public class AnnouncementMoreActivity extends BaseActivity {

    @BindView(R.id.iv_back)
    ImageView ivBack;
    @BindView(R.id.rv_recyclerview)
    RecyclerView rvRecyclerview;
    @BindView(R.id.refreshLayout)
    SmartRefreshLayout refreshLayout;
    @BindView(R.id.tv_name)
    TextView tvName;
    private AnnouncementAdapter mAdapter;
    private int count = 1;
    private int lastSize;
    private int nowSize;
    private int delayMillis = 1000;
    private List<WcmBean.ListDatasBean> mList = new ArrayList<>();
    private String moreUrl;
    private String name;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_announcement_more);
        ButterKnife.bind(this);
        setView();
        initData();
    }

    private void initData() {
        HttpMethods.getInstance().info.getWcmData(moreUrl)
                .subscribeOn(Schedulers.io())
                .observeOn(AndroidSchedulers.mainThread())
                .subscribe(new Observer<WcmBean>() {
                    @Override
                    public void onSubscribe(Disposable d) {

                    }

                    @Override
                    public void onNext(WcmBean wcmBean) {
                        if (mList != null) {
                            mList.clear();
                        }
                        mList.addAll(wcmBean.getList_datas());
                    }

                    @Override
                    public void onError(Throwable e) {

                    }

                    @Override
                    public void onComplete() {
                        mAdapter.notifyDataSetChanged();
                    }
                });
    }

    private void setView() {
        moreUrl = getIntent().getStringExtra("moreUrl");
        name = getIntent().getStringExtra("name");
        tvName.setText(name);
        rvRecyclerview.setNestedScrollingEnabled(false);
        rvRecyclerview.setLayoutManager(new LinearLayoutManager(activity));
        mAdapter = new AnnouncementAdapter(R.layout.item_announcement, mList);
        rvRecyclerview.setAdapter(mAdapter);
        refreshLayout.setDisableContentWhenRefresh(true);//是否在刷新的时候禁止列表的操作
        refreshLayout.setDisableContentWhenLoading(true);//是否在加载的时候禁止列表的操作
        rvRecyclerview.addOnItemTouchListener(new OnItemClickListener() {

            @Override
            public void onSimpleItemClick(BaseQuickAdapter adapter, View view, int position) {
                Bundle b = new Bundle();
                b.putString("webUrl",mList.get(position).getUrl());
                ActivityUtils.startActivity(b,activity,UserAgreementActivity.class);
            }
        });
        //设置下拉刷新监听事件
        refreshLayout.setOnRefreshListener(new OnRefreshListener() {
            @Override
            public void onRefresh(@NonNull final RefreshLayout refreshLayout) {
                downRefresh();
            }
        });

        refreshLayout.setOnLoadMoreListener(new OnLoadMoreListener() {
            @Override
            public void onLoadMore(@NonNull final RefreshLayout refreshLayout) {
                onLoadMoreRequested();
            }
        });
    }

    /**
     * 下拉刷新
     */
    public void downRefresh() {
        count = 1;
        new Handler().postDelayed(new Runnable() {
            @Override
            public void run() {
                refreshLayout.setNoMoreData(false);
                initData();
                refreshLayout.finishRefresh();
            }
        }, delayMillis);
    }


    /**
     * 上拉加载
     */
    public void onLoadMoreRequested() {
        final String countUrl = moreUrl.replace("next_133", "next_133_" + count);
        lastSize = mList.size();
        new Handler().postDelayed(new Runnable() {
            @Override
            public void run() {
                HttpMethods.getInstance().info.getWcmData(countUrl)
                        .subscribeOn(Schedulers.io())
                        .observeOn(AndroidSchedulers.mainThread())
                        .subscribe(new Observer<WcmBean>() {
                            @Override
                            public void onSubscribe(Disposable d) {
                                addDisposable(d);
                            }

                            @Override
                            public void onNext(WcmBean wcmBean) {
                                mList.addAll(wcmBean.getList_datas());
                                nowSize = mList.size();
                                if (nowSize > lastSize) {
                                    count++;
                                }
                            }

                            @Override
                            public void onError(Throwable e) {
                                Log.i("999", "onError: " + e.toString());
//                                if (NetUtil.isNetworkConnected(activity)){
//                                    myAdapter.loadMoreEnd(mLoadMoreEndGone);
//                                }else{
//                                    myAdapter.loadMoreFail();
////                                    swipeLayout.setEnabled(true);
//                                }
                                if ("HTTP 404 Not Found".equals(e.getMessage())) {
                                    refreshLayout.finishLoadMoreWithNoMoreData();
                                }
                            }

                            @Override
                            public void onComplete() {
                                mAdapter.notifyDataSetChanged();
                                refreshLayout.finishLoadMore();
                            }
                        });
            }
        }, delayMillis);
    }

    @OnClick(R.id.iv_back)
    public void onViewClicked() {
        finish();
    }
}
